package com.beiing.tianshuai.tianshuai.mine.ui;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.adapter.MyAttentionAdapter;
import com.beiing.tianshuai.tianshuai.base.BaseActivity;
import com.beiing.tianshuai.tianshuai.entity.FollowResultBean;
import com.beiing.tianshuai.tianshuai.entity.MyAttentionBean;
import com.beiing.tianshuai.tianshuai.mine.presenter.MyAttentionPresenter;
import com.beiing.tianshuai.tianshuai.mine.view.MyAttentionViewImpl;
import com.beiing.tianshuai.tianshuai.util.LogUtils;
import com.beiing.tianshuai.tianshuai.widget.MyRefreshHeader;
import com.beiing.tianshuai.tianshuai.widget.RecycleViewDivider;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class MyAttentionActivity extends BaseActivity implements MyAttentionViewImpl {

    @BindView(R.id.txt_empty_warning)
    TextView mEmptyWarning;                                 // 空警告
    @BindView(R.id.toolbar_navigation)
    ImageView mToolbarNavigation;                           // 返回
    @BindView(R.id.toolbar)
    Toolbar mToolbar;                                       // Toolbar
    @BindView(R.id.toolbar_title)
    TextView mTitle;                                        // Toolbar标题
    @BindView(R.id.srl_my_refresh_layout)
    SmartRefreshLayout mRefreshLayout;                      // 刷新控件
    @BindView(R.id.rv_my_attention)
    RecyclerView attentionList;                             // 我关注的用户列表

    private static final String TAG = "MyAttentionActivity";
    private int                                                 pageType; // 0 - 我的关注；1 - 我的粉丝
    private Context                                             mContext;
    private String                                              mUid;
    private Unbinder                                            mUnbinder;
    private MyAttentionPresenter                                mPresenter;
    private List<MyAttentionBean.DataBean.ConcernBean>          mAttentionList;
    private MyAttentionAdapter                                  mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_attention;
    }

    @Override
    protected void init() {
        Intent intent = getIntent();
        pageType = intent.getIntExtra("pageType", 0);
        mUid = intent.getStringExtra("uid");
        mContext = this;
        mUnbinder = ButterKnife.bind(this);
        initToolbar();
        initRefreshLayout();
        initRecyclerView();
        initListener();
        initPresenter();
    }

    /**
     * 初始化toolbar
     */
    private void initToolbar() {
        mToolbarNavigation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        switch (pageType) {
            case 0:
                mTitle.setText("我的关注");
                break;
            case 1:
                mTitle.setText("我的粉丝");
                break;
        }
    }

    /**
     * 初始化刷新控件
     */
    private void initRefreshLayout() {
        mRefreshLayout.setRefreshHeader(new MyRefreshHeader(mContext));
        mRefreshLayout.setEnableLoadmore(false);
        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                switch (pageType) {
                    case 0:
                        mPresenter.getMyAttentionResult(mUid);
                        break;
                    case 1:
                        mPresenter.getMyFansResult(mUid);
                        break;
                }
            }
        });
    }

    /**
     * 初始化RecyclerView并填充数据
     */
    private void initRecyclerView() {
        mAttentionList = new ArrayList<>();
        mAdapter = new MyAttentionAdapter(mContext, mAttentionList);
        attentionList.setAdapter(mAdapter);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        attentionList.setNestedScrollingEnabled(false);
        attentionList.setLayoutManager(linearLayoutManager);
        //绘制item间的分割线，
        attentionList.addItemDecoration(new RecycleViewDivider(
                mContext, LinearLayoutManager.HORIZONTAL, 1, ContextCompat.getColor(mContext, R.color.divider_gray_light_efefef)));
    }

    private void initListener() {
        mAdapter.setListener(new MyAttentionAdapter.OnItemClickListener() {
            @Override
            public void onItemClickListener(int position, String fromUid, String toUid) {
                switch (pageType) {
                    case 0:
                        Intent followedIntent = new Intent(mContext, NewPersonalIndexActivity.class);
                        followedIntent.putExtra("uid", toUid);
                        LogUtils.i(toUid + "/" + fromUid, "uid");
                        mContext.startActivity(followedIntent);
                        break;
                    case 1:
                        Intent fansIntent = new Intent(mContext, NewPersonalIndexActivity.class);
                        fansIntent.putExtra("uid", fromUid);
                        LogUtils.i(toUid + "/" + fromUid, "uid");
                        mContext.startActivity(fansIntent);
                        break;
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        switch (pageType) {
            case 0:
                mPresenter.getMyAttentionResult(mUid);
                break;
            case 1:
                mPresenter.getMyFansResult(mUid);
                break;
        }
    }

    /**
     * 初始化presenter
     */
    private void initPresenter() {
        mPresenter = new MyAttentionPresenter(this);
    }

    @Override
    public void showProgress() {
    }

    @Override
    public void hideProgress() {
    }

    @Override
    public void onRequestSuccess(MyAttentionBean bean) {
        if (mRefreshLayout.isRefreshing()) {
            mRefreshLayout.finishRefresh();
        }
        List<MyAttentionBean.DataBean.ConcernBean> concern = bean.getData().getConcern();
        if (concern != null && concern.size() > 0) {
            mEmptyWarning.setVisibility(View.GONE);
            mAttentionList.clear();
            mAttentionList.addAll(concern);
            mAdapter.notifyDataSetChanged();
            LogUtils.i("onRequestSuccess: " + bean.getData().toString(), TAG);
        } else {
            mAttentionList.clear();
            mAdapter.notifyDataSetChanged();
            mEmptyWarning.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onRequestFailed(Throwable throwable) {
        if (mRefreshLayout.isRefreshing()) {
            mRefreshLayout.finishRefresh();
        }
        LogUtils.e(throwable.toString(), TAG);
        showShortText("网络异常");
    }

    @Override
    protected void onDestroy() {
        mUnbinder.unbind();
        super.onDestroy();
    }

    @Override
    public void onFollowSuccess(FollowResultBean bean, int position, MyAttentionBean.DataBean.ConcernBean concernBean) {

    }

    @Override
    public void onFollowError(Throwable throwable) {

    }
}
